<?php
class ymdian_orderControl extends skymvc{
	private $admin;
	public function __construct(){
		parent::__construct(); 
	}
	
	public function onInit(){
		$this->loadConfig("table");
		$this->admin=M("login")->getAdmin();
	}
	
	public function onDefault(){
		$where=$GLOBALS['sw']." AND status<99 ";
		$url=APPADMIN."?m=ymdian_order";
		$start=get('per_page','i');
		$limit=20;
		
		$orderno=get('orderno','h');
		if($orderno){
			$where.=" AND orderno='".$orderno."' ";
			$url.="&orderno=$orderno";
		}
		
		$nickname=get('ninckname','h');
		if($nickname){
			$user=M("user")->selectRow(array("where"=>"nickname='".$nickname."' "));
			if($user){
				$where.=" userid=".$user['userid'];
			}else{
				$where.=" 1=2 ";			
			}
			$url.="&nickname=".urlencode($nickname);
		}
		 
		$status=get('status','i');
		if($status>=0){
			$where.=" AND status=$status";
		}
			$url.="&status=$status";
			
		if(!isset($_GET['s_ispay'])){
			$ispay=-1;
		}else{
			$ispay=get('s_ispay','i');
		}
		if($ispay>=0){
			$where.=" AND ispay=$ispay";
			$url.="&s_ispay=$ispay"; 
		}
		
		$paytype=get('paytype','h');
		if(!empty($paytype)){
			$url.="&paytype=".$paytype;
			if($paytype=='hdfk'){
				$where.=" AND unpay=1";
			}elseif($paytype=='unpay'){
				$where.=" AND (unpay=0 AND ispay=1)";
			}else{
				$where.=" AND paytype='".$paytype."'";
			}
		}
		
		$start_time=get('start_time','h');
		$end_time=get('end_time','h');
		if($start_time){
			$where.=" AND dateline>".strtotime($start_time)." ";
			$url.="&start_time=".$start_time;
		}
		
		if($end_time){
			$where.=" AND dateline<".strtotime($end_time)." ";
			$url.="&end_time=".$end_time;
		}
		 
		$sc_id=get('sc_id','i');
		if($sc_id){
			$scids=M("site_city")->id_family($sc_id);
			$where.=" AND sc_id in("._implode($scids).")";
			$url.="&sc_id=".$sc_id;
		}
		$option=array(
			"where"=>$where,
			"order"=>"order_id DESC",
			"start"=>$start,
			"limit"=>$limit
		);
		$total_money=M("ymdian_order")->selectOne(array(
			"where"=>$where,
			"fields"=>" sum(money) as m "
		));
		$rscount=true;
		$data=M("ymdian_order")->select($option,$rscount);
		$page_total_money=0;
		if($data){
			
			foreach($data as $k=>$v){
				$page_total_money+=$v['money'];
				$shopids[]=$v['shopid'];
			}
			$shops=M("ymdian")->getShopByIds($shopids);
			foreach($data as $k=>$v){
				$v['shop_name']=$shops[$v['shopid']]['title'];
				$v['address']=M("ymdian_order_address")->selectRow(array("where"=>"order_id=".$v['order_id'],"order"=>"id DESC"));
				$data[$k]=$v;
			}
		}
		$pagelist=$this->pagelist($rscount,$limit,$url);
		$order_status_list=$this->config_item('order_status_list');
		$order_type_list=$this->config_item('order_type_list');
		$order_ispay=$this->config_item('order_ispay');
		$this->smarty->assign(array(
			"data"=>$data,
			"rscount"=>$rscount,
			"pagelist"=>$pagelist,
			"order_status_list"=>$order_status_list,
			"order_type_list"=>$order_type_list,
			"order_ispay"=>$order_ispay,
			"total_money"=>round($total_money,2),
			"page_total_money"=>$page_total_money
		));
		$this->smarty->display("ymdian_order/index.html");
	}
	/*
	*订单详情
	*/
	public function onShow(){
		$order_id=get('order_id','i');
		$data=M("ymdian_order")->selectRow(array("where"=>"order_id=".$order_id));
		if(empty($data)) $this->goall("参数出错",1);
		$addr=M("ymdian_order_address")->selectRow(array("where"=>"order_id=".$order_id,"order"=>"id DESC"));
		$order_status_list=$this->config_item('order_status_list');
		$order_type_list=$this->config_item('order_type_list');
		$order_ispay=$this->config_item('order_ispay');
		//获取商品
		$order_product=M("ymdian_order_product")->select(array("where"=>"order_id=".$data['order_id']));
		foreach($order_product as $k=>$v){
			$p=M('product')->selectRow(array("where"=>"id=".$v['productid']));
			$p['order_price']=$v['price'];
			$p['iscomment']=$v['iscomment'];
			$p['rating_grade']=$v['rating_grade'];
			$p['amount']=$v['amount'];
			
			$ks=M('product_ks')->selectRow("id=".$v['ksid']);
			if($ks){
				$p['ks_title']=$ks['title'];
				$p['price']=$ks['price'];
			}
			$order_product[$k]=$p;
		}
		$shop=M("ymdian")->selectRow("id=".$data['shopid']);
		$this->smarty->assign(array(
			"data"=>$data,
			"addr"=>$addr,
			"order_status_list"=>$order_status_list,
			"order_type_list"=>$order_type_list,
			"order_ispay"=>$order_ispay,
			"order_product"=>$order_product,
			"admin"=>$this->admin,
			"shop"=>$shop
		));
		$this->smarty->display("ymdian_order/show.html");
		
	}
 

	 
	
}
?>